package org.qiyu.live.api.controller;

import jakarta.annotation.Resource;
import org.qiyu.live.api.service.ILivingRoomService;
import org.qiyu.live.api.vo.req.LivingRoomReqVO;
import org.qiyu.live.api.vo.req.OnlinePkReqVO;
import org.qiyu.live.common.interfaces.vo.WebResponseVO;
import org.qiyu.live.web.starter.context.QiyuRequestContext;
import org.qiyu.live.web.starter.error.ErrorAssert;
import org.qiyu.live.web.starter.error.QiYuBaseErrorEnum;
import org.qiyu.live.web.starter.limit.RequestLimit;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import qiyu.live.gift.dto.RedPacketReceiveDTO;

/**
 * Created with IntelliJ IDEA.
 * Description: 直播间的相关业务
 * User: sfx
 * Date: 2023-10-24
 * Time: 19:41
 */
@RestController
@RequestMapping("/living")
public class LivingRoomController {

    @Resource
    private ILivingRoomService livingRoomService;

    /**
     * 查询直播列表
     *
     * @param livingRoomReqVo 请求vo
     * @return 返回当前的直播列表
     */
    @PostMapping("/list")
    public WebResponseVO list(LivingRoomReqVO livingRoomReqVo) {
        if (livingRoomReqVo == null || livingRoomReqVo.getType() == null) {
            return WebResponseVO.bizError("直播间类型有误");
        }
        if (livingRoomReqVo.getPageSize() < 0 || livingRoomReqVo.getPageSize() > 100) {
            return WebResponseVO.bizError("分页参数异常");
        }
        return WebResponseVO.success(livingRoomService.list(livingRoomReqVo));
    }

    /**
     * 主播开启直播间
     *
     * @param type 直播间的类型
     * @return 返回成功还是失败
     */
    @PostMapping("/startLiving")
    public WebResponseVO startLiving(Integer type) {
        if (type == null) {
            return WebResponseVO.bizError("直播间类型不能为空");
        }
        boolean status = livingRoomService.startLivingRoom(type);
        if (status) {
            return WebResponseVO.success();
        }
        return WebResponseVO.bizError("开启直播失败");
    }

    /**
     * 关闭直播间
     *
     * @param roomId 直播间的id
     * @return 关闭直播间
     */
    @PostMapping("/closeLiving")
    public WebResponseVO closeLiving(Integer roomId) {
        if (roomId == null) {
            return WebResponseVO.bizError("roomId不存在");
        }
        boolean status = livingRoomService.closeLivingRoom(roomId);
        if (status) {
            return WebResponseVO.success();
        }
        return WebResponseVO.bizError("关闭直播失败");
    }

    /**
     * 当用户点击直播pk的时候,会触发这个接口
     *
     * @param onlinePkReqVO 在线pk的另一方
     * @return 返回是否成功
     */
    @PostMapping("/onlinePk")
    public WebResponseVO onlinePk(OnlinePkReqVO onlinePkReqVO) {
        ErrorAssert.isNotNull(onlinePkReqVO.getRoomId(), QiYuBaseErrorEnum.PARAM_ERROR);
        return WebResponseVO.success(livingRoomService.onlinePk(onlinePkReqVO));
    }

    /**
     * 获取主播相关配置信息（只有主播才会有权限）
     *
     */
    //@RequestLimit(limit = 1,second = 10)
    @PostMapping("/anchorConfig")
    public WebResponseVO anchorConfig(Integer roomId) {
        return WebResponseVO.success(livingRoomService.anchorConfig(QiyuRequestContext.getUserId(), roomId));
    }

    /**
     * 初始化红包数据
     *
     */
    @PostMapping("/prepareRedPacket")
    public WebResponseVO prepareRedPacket(LivingRoomReqVO livingRoomReqVO) {
        return WebResponseVO.success(livingRoomService.prepareRedPacket(livingRoomReqVO.getRoomId(),
                livingRoomReqVO.getAnchorId()));
    }

    /**
     * 开启红包雨
     *
     */
    @PostMapping("/startRedPacket")
    public WebResponseVO startRedPacket(LivingRoomReqVO livingRoomReqVO) {
        return WebResponseVO.success(livingRoomService.startRedPacket(QiyuRequestContext.getUserId(),
                livingRoomReqVO.getRedPacketConfigCode()));
    }

    /**
     * 领取红包
     *
     */
    @PostMapping("/getRedPacket")
    public WebResponseVO getRedPacket(LivingRoomReqVO livingRoomReqVO) {
        return WebResponseVO.success(livingRoomService.getRedPacket(QiyuRequestContext.getUserId(),
                livingRoomReqVO.getRedPacketConfigCode()));
    }
}